package org.millioncall.yueyoga.admin.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.ClazzDAO;
import org.millioncall.yueyoga.admin.dao.entity.ClassEntity;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.ClassMemberModel;
import org.millioncall.yueyoga.admin.model.ClassProduct;
import org.millioncall.yueyoga.admin.model.ClassRecord;
import org.millioncall.yueyoga.admin.model.Clazz;
import org.millioncall.yueyoga.admin.model.TinyClass;
import org.millioncall.yueyoga.admin.service.advancedsearch.AviatorResultHandler;

public class ClazzDAOImpl extends BaseDao implements ClazzDAO {

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return this.getPagableSqlSessionTemplate().delete("deleteByPrimaryKey", id);
	}

	@Override
	public int insert(Clazz record) {
		return this.getPagableSqlSessionTemplate().insert("insertClass", record);
	}

	@Override
	public int insertSelective(Clazz record) {
		return this.getPagableSqlSessionTemplate().insert("insertSelective", record);
	}

	@Override
	public Clazz selectByPrimaryKey(Integer id) {
		return this.getPagableSqlSessionTemplate().selectOne("selectClazzByPrimaryKey", id);
	}

	@Override
	public int updateByPrimaryKeySelective(Clazz record) {
		return this.getPagableSqlSessionTemplate().update("updateByPrimaryKeySelective", record);
	}

	@Override
	public int updateByPrimaryKeyWithBLOBs(Clazz record) {
		return this.getPagableSqlSessionTemplate().update("updateByPrimaryKeyWithBLOBs", record);
	}

	@Override
	public int updateByPrimaryKey(Clazz record) {
		return this.getPagableSqlSessionTemplate().update("updateClazz", record);
	}

	@Override
	public PaginatedList<ClassRecord> findClazzPage(Integer start, Integer limit,
			Map<String, Object> map) {
		PreparedQuery<ClassRecord> pq = this.getPagableSqlSessionTemplate().findAll(
				"findClassInPage", map);
		PaginatedList<ClassRecord> pl = pq.list(new PaginateParams(new StartLimitPairImpl(start,
				limit)));
		return pl;
	}

	@Override
	public Clazz findById(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("findClazzById", map);
	}

	@Override
	public ClassEntity findByClassId(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("findClassEntityByClassId", map);
	}

	@Override
	public int deleteClazzByIds(List<Integer> clazzIds) {
		// 删除课程
		this.getPagableSqlSessionTemplate().delete("deleteSchedulesByClassIds", clazzIds);
		// --获取班级下的所有课程
		List<String> scheduleIdList = this.getPagableSqlSessionTemplate().selectList(
				"findScheduleIdsByClassIds", clazzIds);
		// --删除新课程下的席位
		if (scheduleIdList != null && !scheduleIdList.isEmpty()) {
			this.getPagableSqlSessionTemplate().delete("deleteResvsByScheduleIds", scheduleIdList);
		}
		// --删除新课程
		this.getPagableSqlSessionTemplate().delete("deleteNewSchedulesByClassIds", clazzIds);
		// 删除班级会员，实际上某些班级是没有会员的
		this.getPagableSqlSessionTemplate().delete("deleteAllMemberOfClasses", clazzIds);

		// 删除班级产品
		this.getPagableSqlSessionTemplate().delete("deleteAllProductOfClasses", clazzIds);

		// 删除班级
		return this.getPagableSqlSessionTemplate().delete("deleteClazzByIds", clazzIds);
	}

	@Override
	public String findClassCode() {
		return this.getPagableSqlSessionTemplate().selectOne("findClassCode");
	}

	@Override
	public boolean saveClassEntity(ClassEntity entity) {
		return this.getPagableSqlSessionTemplate().insert("insertClass", entity) > 0;
	}

	@Override
	public boolean updateClassEntity(ClassEntity entity) {
		return this.getPagableSqlSessionTemplate().insert("updateClass", entity) > 0;
	}

	@Override
	public boolean addMemberToClass(Integer classId, Integer memberId, String createdById) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("classId", classId);
		params.put("memberId", memberId);
		params.put("createdBy", createdById);
		params.put("createTime", new Date());
		return this.getPagableSqlSessionTemplate().insert("insertClassMember", params) > 0;
	}

	@Override
	public boolean addProductToClass(Integer classId, Integer productId, String productName,
			String createdById) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("classId", classId);
		params.put("productId", productId);
		params.put("productName", productName);
		params.put("createdBy", createdById);
		params.put("createTime", new Date());
		return this.getPagableSqlSessionTemplate().insert("insertClassProduct", params) > 0;
	}

	@Override
	public int countOfMember(Integer classId, Integer memberId) {
		Map<String, Integer> params = new HashMap<String, Integer>();
		params.put("classId", classId);
		params.put("memberId", memberId);
		return this.getPagableSqlSessionTemplate().selectOne("getCountOfMember", params);
	}

	@Override
	public int countOfProduct(Integer classId, Integer productId) {
		Map<String, Integer> params = new HashMap<String, Integer>();
		params.put("classId", classId);
		params.put("productId", productId);
		return this.getPagableSqlSessionTemplate().selectOne("getCountOfProduct", params);
	}

	@Override
	public PaginatedList<ClassMemberModel> findClassMembersByClassId(Map<String, Object> map,
			int start, int limit) {

		PreparedQuery<ClassMemberModel> pq = this.getPagableSqlSessionTemplate().findAll(
				"findClassMembers", map);
		PaginatedList<ClassMemberModel> pl = pq.list(new PaginateParams(new StartLimitPairImpl(
				start, limit)));
		return pl;
	}

	@Override
	public boolean deleteMemberOfClass(Integer classId, List<Integer> memberIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("classId", classId);
		params.put("memberIds", memberIds);
		return this.getPagableSqlSessionTemplate().delete("deleteMemberOfClass", params) > 0;
	}

	@Override
	public boolean deleteProductOfClass(Integer classId, List<Integer> productIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("classId", classId);
		params.put("productIds", productIds);
		return this.getPagableSqlSessionTemplate().delete("deleteProductOfClass", params) > 0;
	}

	@Override
	public List<ClassProduct> findClassProduct(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findAllClassProduct", map);
	}

	@Override
	public List<TinyClass> findAllClassTiny(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findAllClassTiny", map);
	}

	@Override
	public List<Clazz> findClassByProductId(Integer id) {
		return this.getPagableSqlSessionTemplate().selectList("findClassByProductId", id);
	}

	@Override
	public List<ClassRecord> getAllClazzWithAviator(Map<String, Object> params,
			AviatorExpression expression, Map<String, Object> aviatorParams, int limit) {
		AviatorResultHandler<ClassRecord> handler = new AviatorResultHandler<ClassRecord>(
				expression, aviatorParams, limit);
		this.getPagableSqlSessionTemplate().select("findClassInPage", params,
				new RowBounds(0, Integer.MAX_VALUE), handler);
		return handler.getResultList();
	}

	@Override
	public List<Clazz> findWeiXinClassByMap(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findWeiXinClassByMap", map);
	}
	
}
